package com.yoyo.web.action;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;

import com.yoyo.biz.WeiboBiz;
import com.yoyo.entity.Weibo;

@Controller
public class WeiboAction {
	
	@Resource
	private WeiboBiz weibobiz;
	private List<Weibo> weibos;
	private Weibo weibo;
	private String json;
	
	public WeiboBiz getWeibobiz() {
		return weibobiz;
	}
	public void setWeibobiz(WeiboBiz weibobiz) {
		this.weibobiz = weibobiz;
	}
	public List<Weibo> getWeibos() {
		return weibos;
	}
	public void setWeibos(List<Weibo> weibos) {
		this.weibos = weibos;
	}
	public Weibo getWeibo() {
		return weibo;
	}
	public void setWeibo(Weibo weibo) {
		this.weibo = weibo;
	}
	public String getJson() {
		return json;
	}
	public void setJson(String json) {
		this.json = json;
	}
	
	public String showAllWeibo(){
		weibos = weibobiz.findAll();
		return "json";
	}
	
	public String showFiveWeibo(){
		LinkedHashMap<String,String> params = new LinkedHashMap<String,String>();
		params.put("id", "desc");
		weibos = weibobiz.findAll(null,null,params);
		weibos = weibos.subList(0, 5);
		return "json";
	}
	
	public String saveWeibo(){
		//Map seesion = (Map)ActionContext.getContext().getSession().get("user");
		JSONObject jsonObj = JSONObject.fromObject(json);
		//weibo.setUid((Integer)seesion.get("id"));
		weibo = new Weibo();
		weibo.setText(jsonObj.get("weiboText").toString());
		weibo.setTime(new Date());
		weibobiz.save(weibo);
		return "json";
	}
	public String republicWeibo(){
		//Map seesion = (Map)ActionContext.getContext().getSession().get("user");
		JSONObject jsonObj = JSONObject.fromObject(json);
		//weibo.setUid((Integer)seesion.get("id"));
		weibo = weibobiz.find(jsonObj.get("wid").toString());
		weibo.setTime(new Date());
		weibobiz.save(weibo);
		return "json";
	}
}
